import Vue from 'vue'
import Router from 'vue-router'
// import HelloWorld from '@/components/HelloWorld'
const layout = () => import('../pages/layout.vue')
const index = () => import('../pages/index.vue')
const menu = () => import('../pages/menu')
const menuInfo = () => import('../pages/menu/info.vue')
const role = () => import('../pages/role')
const admin = () => import('../pages/admin')
const category = () => import('../pages/category')
const goods = () => import('../pages/goods')
const specs = () => import('../pages/specs')
const login = () => import('../pages/login')


Vue.use(Router)

const router = new Router({
  mode: 'history',
  routes: [
    {
      path: '/',
      name: 'layout',
      component: layout,
      children: [
        // 首页
        { path: '', component: index },
        // 菜单列表
        { path: 'menu', component: menu },
        // 添加菜单
        { path: 'menu/add', component: menuInfo },
        // 修改菜单
        { path: 'menu/:id', component: menuInfo },
        { path: 'role', component: role },
        { path: 'admin', component: admin },
        { path: 'category', component: category },
        { path: 'goods', component: goods },
        { path: 'specs', component: specs },
      ]
    },
    {
      path: '/login',
      component: login
    }
  ]
})
// 路由守卫
router.beforeEach((to, from, next) => {
  if (to.path != '/login' && to.path != '/') {
    //  既不是登录页也不是首页，就需要登录
    let isLogin = JSON.parse(localStorage.getItem('userInfo'));
    if (isLogin) {
      // 已登录
      next();
    } else {
      // 未登录
      next('/login');
    }
  } else {
    //  是去登录页或者首页，不拦截
    next()
  }

})
export default router